#ifdef MBM_IMIBASE_SUPPORT

#include "mbm_mtk_inc.h"

#include "mbm_typedefine.h"

#include "mbm_imibase_res.h"
#include "mbm_sys_adapter.h"
#include "mbm_vui_adapter.h"
#include "mbm_am_adapter.h"
#include "mbm_account_adapter.h"
#include "mbm_font_adapter.h"
#include "mbm_memory_adapter.h"


#include "mbm_export.h"
#include "mbm_config.h"

#if defined(MBM_DEBUG_IMI)
#define IMI_TRACE mbm_trace
#else
#define IMI_TRACE(...) 
#endif

#define MBM_MAX_APPID_LEN   4

typedef struct
{
    LOCAL_PARA_HDR
	void *func;
	void *param;
} t_mbm_relay_call_msg;

typedef struct
{
    LOCAL_PARA_HDR
	unsigned int iMsg;//MBM_APPLET_MSG_E
	unsigned int data1;
	unsigned int data2;
} t_mbm_applet_msg;


static char g_appid[MBM_MAX_APPID_LEN + 1] = {0};
static t_mbm_sys_bill_invite_sms *g_invite_sms = 0;	
static int gImibaseWinId = 0;
static U8 BT_isPowerOn  = 0 ;

static void mbm_imibase_start(ImpCStr appid);
static void mbm_exit_imibase_window(void);
static U8 imibase_window_del_cb(void *data);

extern void imibase_startup(ImpCStr appid, const void *invite);
extern void imibase_setting(void);
extern void imibase_exit(void);

extern void mbm_CreateGdiLayer(void);
extern void mbm_DestoryGdiLayer(void);
extern MMI_BOOL imimw_is_run(void);
extern void mbm_am_scr_reg_key_handler(void);
extern void mbm_am_scr_reg_tp_handler(void);
extern void mbm_sys_adp_init(void (*cb)(void));

#if 0
extern void imp_av_test(Int8 *frame);
static void mbm_av_test(void)
{
	UInt32 size = 0;
	
	mbm_memory_init();
	
	mbm_CreateGdiLayer();
	
	imp_av_test((Int8*)mbm_GetLCDFrameBuffer(&size));
}
#endif

static void mmi_mbm_imitv_entry(void)
{
    IMI_TRACE("mmi_mbm_imitv_entry");
    mbm_imibase_start("16");
}

static void mmi_mbm_imichat_entry(void)
{
    IMI_TRACE("mmi_mbm_imichat_entry");
    mbm_imibase_start("2");
}


static void mmi_mbm_imibook_entry(void)
{
    IMI_TRACE("mmi_mbm_imibook_entry");
    mbm_imibase_start("17");
}

static void mmi_mbm_imiphoto_entry(void)
{
    IMI_TRACE("mmi_mbm_imiphoto_entry");
    mbm_imibase_start("18");
}

static void mmi_mbm_imimusic_entry(void)
{
    IMI_TRACE("mmi_mbm_imimusic_entry");
    mbm_imibase_start("20");
}

static void mmi_mbm_imiaudio_entry(void)
{
    IMI_TRACE("mmi_mbm_imiaudio_entry");
    mbm_imibase_start("25");
}

static void mmi_mbm_imivideo_entry(void)
{
    IMI_TRACE("mmi_mbm_imivideo_entry");
    mbm_imibase_start("23");

}

static void mmi_mbm_imiiptv_entry(void)
{
    IMI_TRACE("mmi_mbm_imiiptv_entry");
    mbm_imibase_start("24");

}

static void mmi_mbm_imiradio_entry(void)
{
    IMI_TRACE("mmi_mbm_imiradio_entry");
    mbm_imibase_start("29");
}



static void mmi_mbm_imisetting_entry(void)
{
    IMI_TRACE("mmi_mbm_imisetting_entry");

    mbm_memory_init();
    
    EntryNewScreen(SCR_ID_MBM_IMIBASE, mbm_exit_imibase_window, mmi_mbm_imisetting_entry,NULL);
    SetDelScrnIDCallbackHandler(SCR_ID_MBM_IMIBASE, imibase_window_del_cb);

    mbm_CreateGdiLayer();
    mdi_audio_suspend_background_play();

    entry_full_screen();

    if(0 == gImibaseWinId)
    {
        gImibaseWinId = SCR_ID_MBM_IMIBASE;
        IMI_TRACE("mbm_start_imibase_window imibase_setting");
        imibase_setting();
    }
    else
    {
        IMI_TRACE("mbm_start_imibase_window get focus");
        mbm_am_excute_state_handler(MBM_APPLET_FOREGROUND);
    }

    mbm_am_scr_reg_key_handler();
    mbm_am_scr_reg_tp_handler();
    
   
}

static void mmi_mbm_imiworld_entry(void)
{

    U16 ItemList[MAX_SUB_MENUS];
    U16 ItemIcons[MAX_SUB_MENUS];
    U16 nItems;
    U8 *guiBuffer;

    EntryNewScreen(SCR_ID_MBM_IMIWORLD, NULL, mmi_mbm_imiworld_entry, NULL);

    guiBuffer = GetCurrGuiBuffer(SCR_ID_MBM_IMIWORLD);
    nItems = GetNumOfChild_Ext(MENU_ID_IMIWORLD_ENTRY);
    GetSequenceStringIds_Ext(MENU_ID_IMIWORLD_ENTRY, ItemList);
    GetSequenceImageIds_Ext(MENU_ID_IMIWORLD_ENTRY, ItemIcons);
    SetParentHandler(MENU_ID_IMIWORLD_ENTRY);
    RegisterHighlightHandler(ExecuteCurrHiliteHandler);

#ifdef __MMI_WGUI_MINI_TAB_BAR__
    wgui_enable_mini_tab_bar(MAIN_MENU_MULTIMEDIA_MENUID);
#endif 


    ShowCategory52Screen(
        STR_ID_MBM_IMIWORLD,
        GetRootTitleIcon(MENU_ID_IMIWORLD_ENTRY),
        STR_GLOBAL_OK,
        IMG_GLOBAL_OK,
        STR_GLOBAL_BACK,
        IMG_GLOBAL_BACK,
        nItems,
        ItemList,
        ItemIcons,
        0,
        0,
        0,
        guiBuffer);

    SetRightSoftkeyFunction(GoBackHistory, KEY_EVENT_UP);
    SetKeyHandler(GoBackHistory, KEY_LEFT_ARROW, KEY_EVENT_DOWN);

}


void mmi_mbm_imitv_highlight_app(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    IMI_TRACE("mmi_mbm_imitv_highlight_app");
    
    ChangeLeftSoftkey(STR_GLOBAL_OK, 0);
    ChangeRightSoftkey(STR_GLOBAL_BACK, 0);

    SetLeftSoftkeyFunction(mmi_mbm_imitv_entry, KEY_EVENT_UP);
    SetRightSoftkeyFunction(GoBackHistory, KEY_EVENT_UP);

    SetKeyHandler(mmi_mbm_imitv_entry, KEY_RIGHT_ARROW, KEY_EVENT_DOWN);
    SetKeyHandler(GoBackHistory, KEY_LEFT_ARROW, KEY_EVENT_DOWN);
}

void mmi_mbm_imichat_highlight_app(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    IMI_TRACE("mmi_mbm_imichat_highlight_app");
    
    ChangeLeftSoftkey(STR_GLOBAL_OK, 0);
    ChangeRightSoftkey(STR_GLOBAL_BACK, 0);

    SetLeftSoftkeyFunction(mmi_mbm_imichat_entry, KEY_EVENT_UP);
    SetRightSoftkeyFunction(GoBackHistory, KEY_EVENT_UP);

    SetKeyHandler(mmi_mbm_imichat_entry, KEY_RIGHT_ARROW, KEY_EVENT_DOWN);
    SetKeyHandler(GoBackHistory, KEY_LEFT_ARROW, KEY_EVENT_DOWN);
}


void mmi_mbm_imiworld_highlight_app(void)
{
    IMI_TRACE("mmi_mbm_imiworld_highlight_app");
    
    ChangeLeftSoftkey(STR_GLOBAL_OK, 0);
    ChangeRightSoftkey(STR_GLOBAL_BACK, 0);

    SetLeftSoftkeyFunction(mmi_mbm_imiworld_entry, KEY_EVENT_UP);
    SetRightSoftkeyFunction(GoBackHistory, KEY_EVENT_UP);

    SetKeyHandler(mmi_mbm_imiworld_entry, KEY_RIGHT_ARROW, KEY_EVENT_DOWN);
    SetKeyHandler(GoBackHistory, KEY_LEFT_ARROW, KEY_EVENT_DOWN);    
}

void mmi_mbm_imibook_highlight_app(void)
{
    IMI_TRACE("mmi_mbm_imibook_highlight_app");
    
    ChangeLeftSoftkey(STR_GLOBAL_OK, 0);
    ChangeRightSoftkey(STR_GLOBAL_BACK, 0);

    SetLeftSoftkeyFunction(mmi_mbm_imibook_entry, KEY_EVENT_UP);
    SetRightSoftkeyFunction(GoBackHistory, KEY_EVENT_UP);

    SetKeyHandler(mmi_mbm_imibook_entry, KEY_RIGHT_ARROW, KEY_EVENT_DOWN);
    SetKeyHandler(GoBackHistory, KEY_LEFT_ARROW, KEY_EVENT_DOWN);    
}

void mmi_mbm_imiphoto_highlight_app(void)
{
    IMI_TRACE("mmi_mbm_imiphoto_highlight_app");
    
    ChangeLeftSoftkey(STR_GLOBAL_OK, 0);
    ChangeRightSoftkey(STR_GLOBAL_BACK, 0);

    SetLeftSoftkeyFunction(mmi_mbm_imiphoto_entry, KEY_EVENT_UP);
    SetRightSoftkeyFunction(GoBackHistory, KEY_EVENT_UP);

    SetKeyHandler(mmi_mbm_imiphoto_entry, KEY_RIGHT_ARROW, KEY_EVENT_DOWN);
    SetKeyHandler(GoBackHistory, KEY_LEFT_ARROW, KEY_EVENT_DOWN);    
}

void mmi_mbm_imimusic_highlight_app(void)
{
    IMI_TRACE("mmi_mbm_imimusic_highlight_app");
    
    ChangeLeftSoftkey(STR_GLOBAL_OK, 0);
    ChangeRightSoftkey(STR_GLOBAL_BACK, 0);

    SetLeftSoftkeyFunction(mmi_mbm_imimusic_entry, KEY_EVENT_UP);
    SetRightSoftkeyFunction(GoBackHistory, KEY_EVENT_UP);

    SetKeyHandler(mmi_mbm_imimusic_entry, KEY_RIGHT_ARROW, KEY_EVENT_DOWN);
    SetKeyHandler(GoBackHistory, KEY_LEFT_ARROW, KEY_EVENT_DOWN);    
}

void mmi_mbm_imiaudio_highlight_app(void)
{
    IMI_TRACE("mmi_mbm_imiaudio_highlight_app");
    
    ChangeLeftSoftkey(STR_GLOBAL_OK, 0);
    ChangeRightSoftkey(STR_GLOBAL_BACK, 0);

    SetLeftSoftkeyFunction(mmi_mbm_imiaudio_entry, KEY_EVENT_UP);
    SetRightSoftkeyFunction(GoBackHistory, KEY_EVENT_UP);

    SetKeyHandler(mmi_mbm_imiaudio_entry, KEY_RIGHT_ARROW, KEY_EVENT_DOWN);
    SetKeyHandler(GoBackHistory, KEY_LEFT_ARROW, KEY_EVENT_DOWN);    
}

void mmi_mbm_imivideo_highlight_app(void)
{
    IMI_TRACE("mmi_mbm_imivideo_highlight_app");
    
    ChangeLeftSoftkey(STR_GLOBAL_OK, 0);
    ChangeRightSoftkey(STR_GLOBAL_BACK, 0);

    SetLeftSoftkeyFunction(mmi_mbm_imivideo_entry, KEY_EVENT_UP);
    SetRightSoftkeyFunction(GoBackHistory, KEY_EVENT_UP);

    SetKeyHandler(mmi_mbm_imivideo_entry, KEY_RIGHT_ARROW, KEY_EVENT_DOWN);
    SetKeyHandler(GoBackHistory, KEY_LEFT_ARROW, KEY_EVENT_DOWN);    
}

void mmi_mbm_imiiptv_highlight_app(void)
{
    IMI_TRACE("mmi_mbm_imiiptv_highlight_app");
    
    ChangeLeftSoftkey(STR_GLOBAL_OK, 0);
    ChangeRightSoftkey(STR_GLOBAL_BACK, 0);

    SetLeftSoftkeyFunction(mmi_mbm_imiiptv_entry, KEY_EVENT_UP);
    SetRightSoftkeyFunction(GoBackHistory, KEY_EVENT_UP);

    SetKeyHandler(mmi_mbm_imiiptv_entry, KEY_RIGHT_ARROW, KEY_EVENT_DOWN);
    SetKeyHandler(GoBackHistory, KEY_LEFT_ARROW, KEY_EVENT_DOWN);    
}

void mmi_mbm_imiradio_highlight_app(void)
{
    IMI_TRACE("mmi_mbm_imiradio_highlight_app");
    
    ChangeLeftSoftkey(STR_GLOBAL_OK, 0);
    ChangeRightSoftkey(STR_GLOBAL_BACK, 0);

    SetLeftSoftkeyFunction(mmi_mbm_imiradio_entry, KEY_EVENT_UP);
    SetRightSoftkeyFunction(GoBackHistory, KEY_EVENT_UP);

    SetKeyHandler(mmi_mbm_imiradio_entry, KEY_RIGHT_ARROW, KEY_EVENT_DOWN);
    SetKeyHandler(GoBackHistory, KEY_LEFT_ARROW, KEY_EVENT_DOWN);    
}

void mmi_mbm_imisetting_highlight_app(void)
{
    IMI_TRACE("mmi_mbm_imisetting_highlight_app");
    
    ChangeLeftSoftkey(STR_GLOBAL_OK, 0);
    ChangeRightSoftkey(STR_GLOBAL_BACK, 0);

    SetLeftSoftkeyFunction(mmi_mbm_imisetting_entry, KEY_EVENT_UP);
    SetRightSoftkeyFunction(GoBackHistory, KEY_EVENT_UP);

    SetKeyHandler(mmi_mbm_imisetting_entry, KEY_RIGHT_ARROW, KEY_EVENT_DOWN);
    SetKeyHandler(GoBackHistory, KEY_LEFT_ARROW, KEY_EVENT_DOWN);    
}



static void mbm_exit_imibase_window(void)
{
    IMI_TRACE("mbm_exit_imibase_window");
    //mbm_DestoryGdiLayer();
    mbm_am_excute_state_handler(MBM_APPLET_BACKGROUND);
    gdi_layer_flatten_previous_to_base(); /*merge current layer to base layer*/
}

static U8 imibase_window_del_cb(void *data)
{
    IMI_TRACE("imibase_window_del_cb");
    
    /*wqw :resume background music play */
    mbm_sound_resume_background_play();
    /*wqw :resume bt */
    if(BT_isPowerOn)
    {
       mmi_bt_reset_hw_on();
    }
    gImibaseWinId = 0;
    imibase_exit();
    mbm_network_deactive();
    mbm_DestoryGdiLayer();
    mbm_memory_deinit();
    return 1;
}

void mbm_start_imibase_window(void)
{
    IMI_TRACE("mbm_start_imibase_window");
    
    EntryNewScreen(SCR_ID_MBM_IMIBASE, mbm_exit_imibase_window, mbm_start_imibase_window,NULL);
    SetDelScrnIDCallbackHandler(SCR_ID_MBM_IMIBASE, imibase_window_del_cb);

    mbm_CreateGdiLayer();
    mdi_audio_suspend_background_play();

    entry_full_screen();

    if(0 == gImibaseWinId)
    {
        gImibaseWinId = SCR_ID_MBM_IMIBASE;
        IMI_TRACE("mbm_start_imibase_window imibase_startup");
        imibase_startup(g_appid, g_invite_sms);
    }
    else
    {
        IMI_TRACE("mbm_start_imibase_window get focus");
        mbm_am_excute_state_handler(MBM_APPLET_FOREGROUND);
    }

    mbm_am_scr_reg_key_handler();
    mbm_am_scr_reg_tp_handler();
}

void mbm_imibase_start(ImpCStr appid)
{
    mbm_assert(strlen(appid) <= MBM_MAX_APPID_LEN, "mbm_imibase_start appid error!");

    IMI_TRACE("mbm_imibase_start");

    strcpy(g_appid, appid);
    mbm_assert(strlen(g_appid) > 0, "appid null");

    // fix mp3 problem.
    mbm_sound_suspend_background_play();
    //wqw : fix BT problem.
    
   BT_isPowerOn = mmi_bt_is_power_on(0);
    if(BT_isPowerOn )
    {
            mmi_bt_reset_hw_off();
            //mmi_bt_close();
    }
    mbm_memory_init();

    mbm_sys_adp_init(mbm_start_imibase_window);	
}

void mbm_open_input_win(void *param)
{
    t_mbm_input_param *pInput = (t_mbm_input_param*)param;
    UInt16 title_ucs2[200] = {0};

    MMI_ASSERT(param);

    mbm_str_conv_utf8_ucs2((const Int8*)pInput->title, (const int)strlen(pInput->title), title_ucs2, 200);
    mbm_input_screen((Int8*)title_ucs2, pInput->im, pInput->msg, pInput->max, pInput->callback, pInput->param);
}

extern void mbm_pb_list_pre_entry_for_number_and_email();
static void MBM_AppletMsgHandle(void* param)
{
	t_mbm_applet_msg* msg = (t_mbm_applet_msg*)param;
	
	IMI_TRACE("MBM_AppletMsgHandle msg->iMsg=%d", msg->iMsg);
	
	switch(msg->iMsg)
	{
	case MBM_APPLET_MSG_AM_EXIT:
		imibase_exit();
		
		if(SCR_ID_MBM_IMIBASE == GetActiveScreenId())
		{
			GoBackHistory();
		}
		else if(imimw_is_run())
		{
			IMI_TRACE("MBM_AppletMsgHandle imimw_is_run");
			//DeleteScreenIfPresent(SCR_ID_MBM_IMIBASE);
		}
		else
		{
			IMI_TRACE("Warning, MBM_AppletMsgHandle can't run to here");
			DeleteScreenIfPresent(SCR_ID_MBM_IMIBASE);
			GoBackHistory();
		}
		break;
	case MBM_APPLET_MSG_INPUT_REQ:
		mbm_open_input_win((void*)(msg->data1));
		break;
	case MBM_APPLET_MSG_INPUT_CLOSE_REQ:
		if(SCR_ID_MBM_INPUT_SCREEN == GetActiveScreenId())
		{
			GoBackHistory();
		}
		else
		{
			DeleteScreenIfPresent(SCR_ID_MBM_INPUT_SCREEN);
			GoBackHistory();
		}
		break;
	case MBM_APPLET_MSG_PB_REQ:
            {
                t_mbm_pb_param* temp = (t_mbm_pb_param*)(((t_mbm_applet_msg*)param)->data1);
                mbm_pb_setcallback(temp->callback,temp->param);
                mbm_pb_list_pre_entry_for_number_and_email();
		break;
          }
	case MBM_APPLET_MSG_PB_CLOSE_REQ:
        GoBackHistory();
		break;
	case MBM_APPLET_MSG_VIDEO_CALL:
		IMI_TRACE("mbm_play_invite_ring");//mbm_app_nofity(MBMSYS_NOTIFY_TYPE_ID_VIDEO_CALL);
		mbm_play_invite_ring(imp_get_cur_sim_card());
		break;
    case MBM_APPLET_MSG_VIDEO_CALL_END:
        mbm_stop_invite_ring();
        break;
	default:
		break;
	}
}


/**
 * @name MBM_PostAppletMessage -discripe
 *
 * @param iMsg	-in	-reference MBM_APPLET_MSG_E
 *
 * @return -discripe
 */
void MBM_PostAppletMessage(UInt32 iMsg, UInt32 data1, UInt32 data2)
{
    ilm_struct* ilm = allocate_ilm((module_type)MOD_MMI);
    t_mbm_applet_msg* msg = (t_mbm_applet_msg*)construct_local_para(sizeof(t_mbm_applet_msg), TD_CTRL);

    mmi_frm_set_protocol_event_handler(MSG_ID_IMIBASE_APPLET_MSG, (PsIntFuncPtr)MBM_AppletMsgHandle, MMI_FALSE);

    msg->iMsg = iMsg;
    msg->data1 = data1;
    msg->data2 = data2;

    IMI_TRACE("MBM_PostAppletMessage msg.iMsg:%d data1:%d, data2:%d", msg->iMsg, msg->data1, msg->data2);

    ilm->src_mod_id = (module_type) MOD_MMI;
    ilm->dest_mod_id = (module_type)MOD_MMI;
    ilm->sap_id = INVALID_SAP;
    ilm->msg_id = (msg_type)MSG_ID_IMIBASE_APPLET_MSG;

    ilm->local_para_ptr = (local_para_struct *)msg;
    ilm->peer_buff_ptr = NULL;

    msg_send_ext_queue(ilm);
}

static void mbm_mmi_relay_call_msg_handler(void *data)
{
    if (data)
    {
        Relay_Fun func = (Relay_Fun)(((t_mbm_relay_call_msg *)data)->func);
        (*func)(((t_mbm_relay_call_msg *)data)->param);
    }
}

/**
 * @name Imibase_post_relay_call_msg -discripe
 *
 * @param param1	-property	-discripe
 *
 * @return -discripe
 */
void Imibase_post_relay_call_msg(void *func, void *param)
{
    ilm_struct* ilm = allocate_ilm((module_type)MOD_MMI);
    t_mbm_relay_call_msg* cnf = (t_mbm_relay_call_msg*)construct_local_para(sizeof(t_mbm_relay_call_msg), TD_CTRL);

    mmi_frm_set_protocol_event_handler(MSG_ID_IMIBASE_RELAY_CALL, (PsIntFuncPtr)mbm_mmi_relay_call_msg_handler, MMI_FALSE);
    
    cnf->func = (void *)func;
    cnf->param = param;

    ilm->src_mod_id = (module_type) MOD_MMI;
    ilm->dest_mod_id = (module_type)MOD_MMI;
    ilm->sap_id = INVALID_SAP;
    ilm->msg_id = (msg_type)MSG_ID_IMIBASE_RELAY_CALL;
    
    ilm->local_para_ptr = (local_para_struct *)cnf;
    ilm->peer_buff_ptr = NULL;

    msg_send_ext_queue(ilm);
}

MMI_BOOL imibase_is_run(void)
{
	if(gImibaseWinId)
	{
		return MMI_TRUE;
	}
    
	return MMI_FALSE;
}

void mbm_set_invite_sms_content(t_mbm_sys_bill_invite_sms * inviteSMS)
{
    if(inviteSMS)
    {
        g_invite_sms = inviteSMS;
    }
}

void mbm_imichat_entry()
{
    mbm_imibase_start("2");
}


#endif//MBM_IMIBASE_SUPPORT
